# coding=utf-8

"""
提供一些公共使用的工具方法
"""
import time


def any_hit(target, element):
    """"检测两个物体发生碰撞函数"""
    # 定义四个开关
    button1 = False
    button2 = False
    button3 = False
    button4 = False
    # 获取传入的类的尺寸
    ele_rect = element.rect
    x1 = element.x
    y1 = element.y
    w1 = ele_rect.w
    h1 = ele_rect.h

    # 碰撞判断
    if x1 < target.x + target.rect.w < x1 + w1 or x1 < target.x < x1 + w1:
        button1 = True
    if y1 < target.y < y1 + h1 or y1 < target.y + target.rect.h < y1 + h1:
        button2 = True
    if target.x < x1 + w1 < target.x + target.rect.w or target.x < x1 < target.x + target.rect.w:
        button3 = True
    if target.y < y1 < target.y + target.rect.h or target.y < y1 + h1 < target.y + target.rect.h:
        button4 = True
    if (button1 and button2) or (button3 and button4):
        return True
    else:
        return False


def delays(delay, time0=time.time()):
    class MyDelay(object):
        """创建一个带参数的类装饰器,用来对函数进行延时执行，每过
           delay时间执行一次
        """

        def __init__(self, fun):
            self.sec = delay
            self.time = time0
            self.__fun = fun

        def __call__(self, *args, **kwargs):
            time1 = time.time()
            if time1 - self.time >= self.sec:
                self.__fun(*args, **kwargs)
                self.time = time1

    return MyDelay


def delay_fps(fps, fps0=0):
    class FpsDelay(object):
        """创建一个根据固定帧数延时执行的类装饰器,每过fps帧数执行一次"""

        def __init__(self, fun):
            self.fps = fps
            self.fps0 = fps0
            self.__fun = fun

        def __call__(self, *args, **kwargs):
            self.fps0 += 1
            # print(self.fps0)
            if self.fps0 % self.fps == 0:
                self.__fun(*args, **kwargs)

    return FpsDelay


def delay_run(fps, fps0=0):
    """创建一个帧数 延时执行的装饰器"""

    def delay(func):
        def new_run(*args, **kwargs):
            nonlocal fps0
            fps0 += 1
            if fps0 % fps == 0:
                res = func(*args, **kwargs)
                return res

        return new_run

    return delay


def delay_time(time1, time0=time.time()):
    """创建一个延时间的装饰器"""

    def delay(func):
        def new_run(*args, **kwargs):
            nonlocal time0
            time2 = time.time()
            if time2 - time0 >= time1:
                time0 = time2
                res = func(*args, **kwargs)
                return res

        return new_run

    return delay


def print_say():
    __a = 0
    print(locals())


if __name__ == "__main__":
    class Person(object):
        def __init__(self, name, age, taste):
            self.name = name
            self._age = age
            self.__taste = taste
            print(locals())
    print(dir(Person))
